<?
include('./header.inc');
include('./database.inc');

function rrmdir($dir) {
   if (is_dir($dir)) {
     $objects = scandir($dir);
     foreach ($objects as $object) {
       if ($object != "." && $object != "..") {
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
     reset($objects);
     rmdir($dir);
   }
}

if (!isset($_GET['sid'])){
	header( 'Location: ./slides.php' ) ;
	die();
}

$sid = mysql_real_escape_string($_GET['sid']);

$query2 = "SELECT * FROM slides";
$result2 = mysql_query($query2);
$num_rows = mysql_num_rows($result2);

if ($num_rows <= 1){
	notify("error", "You must have at least one slide at all times");
	header( 'Location: ./slides.php' ) ;
	die();
}

$query1 = "SELECT * FROM slides WHERE slide_id='$sid'";

$result1 = mysql_query($query1);

$row = mysql_fetch_array( $result1 );

rrmdir("../images/".$row['folder']);

$query = "DELETE FROM slides WHERE slide_id='$sid'";

$result = mysql_query($query);



notify("success", "Slide Deleted");
header( 'Location: ./slides.php' ) ;

?>